package com.lin.blog.controller;

import com.lin.blog.model.BlogInfo;
import com.lin.blog.service.BlogService;
import com.lin.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    //查找所有博客
    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        return blogService.getList();
    }
    //根据博客id，查询博客信息
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("接收参数 getBlogDetail: ："+blogId);
        if(blogId==null){
            throw new UnsupportedOperationException("id不能为null");
        }
        BlogInfo blogInfo=blogService.getBlogDatail(blogId);
        //作者id
        Integer authorId=blogInfo.getUserId();
        //从token中获取用户id
        String userToken=request.getHeader("user_token_header");
        Integer userId= JwtUtils.getUserIdFromToken(userToken);
        if(authorId!=null&&authorId==userId){
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }
    //发布博客 接收json类型
    @RequestMapping("/add")
    public Boolean addBlog(@Validated @RequestBody BlogInfo blogInfo, HttpServletRequest request){
        log.info("接收参数 addBlog: ："+blogInfo);
        //从token中获取用户id
        String userToken=request.getHeader("user_token_header");
        Integer userId= JwtUtils.getUserIdFromToken(userToken);
        blogInfo.setUserId(userId);
        Integer result=null;
        try {
            result=blogService.insertBlog(blogInfo);
            if(result==1){
                return true;
            }
        }catch (Exception e){
            log.error("发布博客失败 e: ",e);
        }
        return true;
    }
    /**
     * 更新博客
     */
    @RequestMapping("/update")
    public Boolean updateBlog(@Validated @RequestBody BlogInfo blogInfo){
        log.info("接收参数 updateBlog: ："+blogInfo);
        Integer result=null;
        try {
            result=blogService.updateBlog(blogInfo);
            if(result>=1){
                return true;
            }
        }catch (Exception e){
            log.error("发送异常 updateBlog: ："+e);
        }
        return false;
    }
    /**
     * 删除博客
     */
    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId){
        Assert.notNull(blogId,"删除的博客id不能为空");
        log.info("接收参数 deleteBlog: ："+blogId);
        Integer result=null;
        try {
            BlogInfo blogInfo=new BlogInfo();
            blogInfo.setId(blogId);
            blogInfo.setDeleteFlag(1);
            result=blogService.updateBlog(blogInfo);
            if(result>=1){
                return true;
            }
        }catch (Exception e){
            log.error("发送异常 updateBlog: ："+e);
        }
        return false;
    }
}
